import 'package:flutter/cupertino.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:mtf/app/base/BaseController.dart';
import 'package:mtf/app/net/HttpManger.dart';
import 'package:mtf/app/widget/VercodeTimerWidget.dart';

import '../../routes/app_pages.dart';
/**
 * GetX Template Generator - fb.com/htngu.99
 * */

class registerController extends BaseController {
  VercodeController verController = VercodeController();
  TextEditingController phoneController = TextEditingController();
  TextEditingController passwordController = TextEditingController();
  TextEditingController password2Controller = TextEditingController();
  TextEditingController yzmController = TextEditingController();
  var _isOneObscure = false.obs;

  set OneObscure(value) => _isOneObscure.value = value;

  get OneObscure => _isOneObscure.value;
  var _isTwoObscure = false.obs;

  set TwoObscure(value) => _isTwoObscure.value = value;

  get TwoObscure => _isTwoObscure.value;
  var _isCheckObs = false.obs;

  set CheckObs(value) => _isCheckObs.value = value;

  get CheckObs => _isCheckObs.value;

  void reqYzm() async {
    var phone = phoneController.text;
    if (phone.isEmpty) {
      print('phoneController${Get.context == null}');
      SmartDialog.showToast("请输入手机号");
    } else {
      showLoading();
      await HttpManager.reqYzm(phone, "reg").then((value) {
        if (value.isSuccess()) {
          verController.vercodeSend();
        }
        hideLoading();
      }, onError: (value) {
        hideLoading();
      });
    }
  }

  void clickNetAgreement() async{
    clickAgreement("useryx");
  }

  void clickPrivacyAgreement() async{
    clickAgreement("ystk");
  }

  void clickRegister() async {
    var phone = phoneController.text;
    var yzm = yzmController.text;
    var password1 = passwordController.text;
    var password2 = password2Controller.text;
    if (phone.isEmpty) {
      print('phoneController${Get.context == null}');
      SmartDialog.showToast("请输入手机号");
    } else if (password1.isEmpty || password2.isEmpty) {
      SmartDialog.showToast("请输入密码");
    } else if (yzm.isEmpty) {
      SmartDialog.showToast("请输入验证码");
    } else if (password1 != password2) {
      SmartDialog.showToast("请确认密码一致性");
    } else if (CheckObs == false) {
      SmartDialog.showToast("请勾选协议");
    } else {
      showLoading();
      await HttpManager.reqRegister(phone, yzm, password1).then((value) {
        hideLoading();
        if (value.isSuccess()) {
          SmartDialog.showNotify(msg: "注册成功", notifyType: NotifyType.success);
          Get.back();
        }
      }, onError: (value) {
        hideLoading();
      });
    }
  }

  void clickAgreement(String type) async {
    showLoading();
    var resp = await HttpManager.reqAgreement();
    if (resp.isSuccessData()) {
      if("ystk"==type){
        Get.toNamed(Routes.AGREE,arguments: {"title":resp.data!.ystk.title,"content":resp.data!.ystk.content});
      }else if("useryx"==type){
        Get.toNamed(Routes.AGREE,arguments: {"title":resp.data!.useryx.title,"content":resp.data!.useryx.content});
      }
    }
    hideLoading();
  }
}
